#pragma once

class CLockCS
{
public:
	CLockCS()	{	InitializeCriticalSection(&m_cs);	}
	~CLockCS()	{	DeleteCriticalSection(&m_cs);	}
	void Lock()	{	EnterCriticalSection(&m_cs);	}
	void Unlock()	{	LeaveCriticalSection(&m_cs);	}
	BOOL TryLock()	{	return (TryEnterCriticalSection(&m_cs) == TRUE);	}

private:
	CRITICAL_SECTION	m_cs;
};

//class CSemaphore
//{
//public:
//	CSemaphore(long lInitialCount, long lMaximumCount)
//	{
//		m_hObject = CreateSemaphore(NULL, lInitialCount, lMaximumCount, NULL);
//	}
//
//	~CSemaphore()	{	CloseHandle(m_hObject);	}
//	virtual bool Unlock(long lCount = 1)
//	{
//		if(lCount >= 1)
//			return (ReleaseSemaphore(m_hObject, lCount, NULL) == TRUE);
//		else
//			return false;
//	}
//
//	HANDLE GetHandle() const {	return m_hObject;	}
//
//private:
//	HANDLE	m_hObject;
//};

class CVarBuffer
{
public:
	CVarBuffer(int nSize = 100 * 1024);
	~CVarBuffer(void);

	virtual BOOL Write(unsigned char* pWrite, unsigned long size);
	virtual BOOL Read(unsigned char* pRead, unsigned long size);
	virtual BOOL TryRead(unsigned char* pRead, unsigned long size);
	void PurgeBuffer();
	int GetSize()	{	return m_nSize;	}

protected:
	int GetDataSize();
	int GetFreeSize();

protected:
	int	m_nSize;
	unsigned char*	m_pBuf;
	unsigned char* m_pRead;
	unsigned char* m_pWrite;
	CLockCS	m_cs;
};
